<template>
  <div class="home-container">
    <van-nav-bar
      title="面包店"
      right-text="购物车"
      @click-right="onClickCart"
    />
    
    <van-tabs v-model:active="activeTab" sticky>
      <van-tab v-for="category in categories" :key="category.id" :title="category.name">
        <div class="product-list">
          <van-card
            v-for="product in filteredProducts"
            :key="product.id"
            :price="product.price.toFixed(2)"
            :title="product.name"
            :thumb="product.image"
          >
            <template #desc>
              <div>{{ product.description }}</div>
            </template>
            <template #footer>
              <van-button size="small" type="primary" @click="addToCart(product)">
                加入购物车
              </van-button>
            </template>
          </van-card>
        </div>
      </van-tab>
    </van-tabs>
    
    <van-tabbar v-model="active" route>
      <van-tabbar-item icon="home-o" to="/home">首页</van-tabbar-item>
      <van-tabbar-item icon="orders-o" to="/orders">订单</van-tabbar-item>
      <van-tabbar-item icon="user-o" to="/profile">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useOrderStore } from '../stores/order'
import { products, categories } from '../mock/products'
import { showToast } from 'vant'

const router = useRouter()
const orderStore = useOrderStore()

const activeTab = ref(0)
const active = ref(0)

const filteredProducts = computed(() => {
  const currentCategory = categories[activeTab.value]
  return products.filter(product => product.category === currentCategory.id)
})

const addToCart = (product) => {
  orderStore.addToCart(product)
  showToast('已加入购物车')
}

const onClickCart = () => {
  router.push('/checkout')
}
</script>

<style scoped>
.home-container {
  min-height: 100vh;
  background-color: #f7f8fa;
  padding-bottom: 50px;
}

.product-list {
  padding: 10px;
}

.van-card {
  margin-bottom: 10px;
  background-color: #fff;
}
</style> 